package com.itmsoft.persistence.config;

import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.cfg.NamingStrategy;

@SuppressWarnings("serial")
public class ItmNamingStrategy extends ImprovedNamingStrategy implements NamingStrategy
{

    @Override
    public String columnName(String columnName)
    {
        return addUnderscores(columnName).toUpperCase();
    }

    @Override
    public String tableName(String tableName)
    {
        return addUnderscores(tableName).toUpperCase();
    }

    @Override
    public String classToTableName(String className)
    {
        return tableName(className);
    }

    @Override
    public String propertyToColumnName(String propertyName)
    {
        return addUnderscores(propertyName).toUpperCase();
    }

    @Override
    public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity,
        String associatedEntityTable, String propertyName)
    {
        return (addUnderscores(ownerEntityTable) + "_TO_" + addUnderscores(associatedEntityTable == null ? propertyName
            : associatedEntityTable)).toUpperCase();
    }

    @Override
    public String foreignKeyColumnName(String propertyName, String propertyEntityName,
        String propertyTableName, String referencedColumnName)
    {
        if (propertyName != null)
        {
            return (addUnderscores(propertyName) + "_ID").toUpperCase();
        }
        return super.foreignKeyColumnName(propertyName, propertyEntityName, propertyTableName,
            referencedColumnName);
    }
}
